package com.foreknow.controller;

import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ManageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //我们只需 要在doGet中获取到请求的参数q=list，然后根据获取到的参数来决定调用哪一个方法
        String method = req.getParameter("q");
        if("delete".equals(method)) {
            delete(req,resp);
        }else if("update".equals(method)){
            update(req,resp);
        }else {
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);

    }

    //查询所有信息的方法
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //调用业务层查询方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.queryInfo();
         PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        session.setAttribute("list",list);
//         将查询的数据保存到服务器   底层是Map k,v
//        req.setAttribute("list",list);
//        将这个请求转达给list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);

    }

    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //调用业务层查询方法
        GuestbookService service = new GuestbookServiceImpl();
        String method = req.getParameter("id");
//        执行删除
        service.delete(Integer.parseInt(method));

        list(req, resp);
    }

    //修改
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");

//        接收数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

//        首先把老的数据删除掉
         //调用业务层查询方法
        GuestbookService service = new GuestbookServiceImpl();
        String method = req.getParameter("id");
         //执行删除
        service.delete(Integer.parseInt(method));
//        传送数据给update页面
        List<Object> list_update = new ArrayList<>();
        Collections.addAll(list_update,id,name,phone,email,title,content);
         //   创建session对象
        HttpSession session = req.getSession();
        session.setAttribute("list_update",list_update);
          // 发送数据
        req.getRequestDispatcher("/update.jsp").forward(req,resp);




    }

}
